package app.modules.menu.application.web;

import app.modules.common.dto.web.WebIdDTO;
import app.modules.menu.business.service.MenuFunctionRelationService;
import app.modules.menu.business.service.MenuService;
import app.modules.menu.business.service.MenuWithHierarchyService;
import app.modules.menu.dto.web.menu.WebAssignFunctionDTO;
import app.modules.menu.dto.web.menu.WebHierarchyMenuDTO;
import app.modules.menu.dto.web.menu.WebHomeMenuDTO;
import app.modules.menu.dto.web.menu.WebMenuDTO;
import app.modules.menu.dto.web.menu.WebMenuSaveDTO;
import app.modules.menu.dto.web.menu.WebMenuUpdateDTO;
import app.modules.security.dto.web.function.WebFunctionDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jasmine.framework.web.entity.WebResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author mh.z
 */
@Tag(name = "菜单API")
@RestController
public class MenuController {
    private MenuService menuService;
    private MenuWithHierarchyService menuWithhierarchyService;
    private MenuFunctionRelationService menuFunctionRelationService;

    public MenuController(MenuService menuService, MenuWithHierarchyService menuWithhierarchyService,
                          MenuFunctionRelationService menuFunctionRelationService) {
        this.menuService = menuService;
        this.menuWithhierarchyService = menuWithhierarchyService;
        this.menuFunctionRelationService = menuFunctionRelationService;
    }

    @Operation(summary = "查询主页菜单")
    @PostMapping("/api/menu/menu/listHomeMenus")
    public ResponseEntity<WebResult<List<WebHomeMenuDTO>>> listHomeMenus() {
        List<WebHomeMenuDTO> menuList = menuWithhierarchyService.listWebHomeMenuDTOs();

        return WebResult.success(menuList).toOkEntity();
    }

    @Operation(summary = "查询带层次结构的菜单")
    @PostMapping("/api/menu/menu/listHierarchyMenus")
    public ResponseEntity<WebResult<List<WebHierarchyMenuDTO>>> listHierarchyMenus(
            @RequestBody WebIdDTO webIdDTO) {
        Long templateId = webIdDTO.getId();
        List<WebHierarchyMenuDTO> menuList = menuWithhierarchyService.listHierarchyMenus(templateId);

        return WebResult.success(menuList).toOkEntity();
    }

    @Operation(summary = "保存菜单")
    @PostMapping("/api/menu/menu/saveMenu")
    public ResponseEntity<WebResult<WebMenuDTO>> saveMenu(@Valid @RequestBody WebMenuSaveDTO saveDTO) {
        WebMenuDTO webMenuDTO = menuService.saveMenu(saveDTO);

        return WebResult.success(webMenuDTO).toOkEntity();
    }

    @Operation(summary = "更新菜单")
    @PostMapping("/api/menu/menu/updateMenu")
    public ResponseEntity<WebResult<WebMenuDTO>> updateMenu(
            @Valid @RequestBody WebMenuUpdateDTO updateDTO) {
        WebMenuDTO webMenuDTO = menuService.updateMenu(updateDTO);

        return WebResult.success(webMenuDTO).toOkEntity();
    }

    @Operation(summary = "删除菜单")
    @PostMapping("/api/menu/menu/deleteMenuById")
    public ResponseEntity<WebResult<Object>> deleteMenuById(
            @RequestBody WebIdDTO paramDTO) {
        Long id = paramDTO.getId();
        menuService.deleteMenuById(id);

        return WebResult.success().toOkEntity();
    }

    @Operation(summary = "查询指定菜单的所有已分配功能")
    @PostMapping("/api/menu/menu/listAssignedFunctions")
    public ResponseEntity<WebResult<List<WebFunctionDTO>>> listAssignedFunctions(
            @RequestBody WebIdDTO paramDTO) {
        Long recordId = paramDTO.getId();
        List<WebFunctionDTO> recordList = menuFunctionRelationService.listAssignedFunctions(recordId);

        return WebResult.success(recordList).toOkEntity();
    }

    @Operation(summary = "向指定菜单分配功能")
    @PostMapping("/api/menu/menu/assignFunctions")
    public ResponseEntity<WebResult<Object>> assignFunctions(
            @RequestBody WebAssignFunctionDTO assignDTO) {
        Long menuId = assignDTO.getMenuId();
        List<Long> functionIdList = assignDTO.getFunctionIds();
        menuFunctionRelationService.assignFunction(menuId, functionIdList);

        return WebResult.success().toOkEntity();
    }

}
